package com.demo.chattest;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.demo.chattest.bean.MessageInfo;
import com.demo.chattest.chat.ChatAdapter;
import com.demo.chattest.databinding.ActivityChatRoomBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package com.demo.chattest
 * @Describe 聊天室
 * @Author wugx
 * @Date 2020-09-25
 */
public class ChatRoomActivity extends AppCompatActivity implements View.OnClickListener {
    private final static String TAG = "ChatRoomActivity";
    Context mContext;
    ActivityChatRoomBinding mBinding;
    private ChatAdapter chatAdapter;

    private boolean addToTop = false;
    private LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mBinding = ActivityChatRoomBinding.inflate(LayoutInflater.from(mContext));
        setContentView(mBinding.getRoot());
        initView();
        addListener();
    }

    private void initView() {
        chatAdapter = new ChatAdapter(new ArrayList<>());
        linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, true);
        mBinding.recyclerView.setLayoutManager(linearLayoutManager);
        chatAdapter.bindToRecyclerView(mBinding.recyclerView);
    }

    private void addListener() {
        mBinding.ivBack.setOnClickListener(this);
        mBinding.btAddReceiver.setOnClickListener(this);
        mBinding.btAddSend.setOnClickListener(this);
        mBinding.btClearAll.setOnClickListener(this);
        mBinding.scAddStart.setOnCheckedChangeListener(mSCAddStartListener);
        mBinding.recyclerView.addOnItemTouchListener(mAdapterListener);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_add_receiver:
                getReceiverList(1);
                break;
            case R.id.bt_add_send:
                getSendList(1);
                break;
            case R.id.bt_clear_all:
                chatAdapter.setNewData(new ArrayList<>());
                break;
        }
    }

    private CompoundButton.OnCheckedChangeListener mSCAddStartListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            addToTop = isChecked;
        }
    };

    private OnItemChildClickListener mAdapterListener = new OnItemChildClickListener() {
        @Override
        public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            MessageInfo messageInfo = chatAdapter.getItem(position);
            if (messageInfo == null) return;
            switch (view.getId()) {
                case R.id.iv_headImg://头像
                    Toast.makeText(mContext, messageInfo.getType() + "==" + messageInfo.getFromUName(), Toast.LENGTH_LONG).show();
                    break;
                case R.id.iv_img://图片
                    Toast.makeText(mContext, messageInfo.getImgUrl(), Toast.LENGTH_LONG).show();
                    break;
                case R.id.fl_audio_contain://语音
                    if (messageInfo.getMediaState() != 1) {
                        messageInfo.setMediaState(1);
                    } else {
                        messageInfo.setMediaState(2);
                    }
                    messageInfo.setMsgRead(1);
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("mediaState", true);
                    bundle.putBoolean("read", true);
                    chatAdapter.notifyItemChanged(position, bundle);
                    break;
                case R.id.iv_video_play://视频
                    Toast.makeText(mContext, messageInfo.getVideoUrl(), Toast.LENGTH_LONG).show();
                    break;
            }

        }
    };

    int j, k;

    private void getSendList(int num) {
        List<MessageInfo> list = new ArrayList<>();
        if (num < 0) return;
        for (int i = 0; i < num; i++) {
            MessageInfo info = new MessageInfo();
            info.setId("s" + i);
            info.setMsgId("s" + System.currentTimeMillis());
            info.setFromUid("1123");
            info.setFromUName("发送者2号");
            info.setTime(System.currentTimeMillis());
            info.setFromHeadImg("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601013182975&di=6cde0f627a7961021d58658536d4dc7f&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F12%2F20200612180401_mvgks.thumb.400_0.jpeg");
            info.setTargetUid("2311");
            info.setTargetUName("接收者1号");
            info.setTargetHeadImg("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601013182974&di=9105e31fe73643913af3edf17695a969&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202008%2F11%2F20200811231800_rqlkt.thumb.400_0.jpeg");
            info.setTargetType(2);
            info.setType(14);
            info.setImgThumbUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601013182973&di=cdc198d83ce7a81b9295f38848593b72&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202003%2F13%2F20200313135704_xlgyg.thumb.400_0.jpeg");
            info.setImgUrl("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=387639067,1599589691&fm=26&gp=0.jpg");
            info.setAudioDuration(140);
            info.setAudioUrl("https://pic.ibaotu.com/19/55/30/11r888piC3Pa.mp3");
            info.setVideoThumbUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601013182973&di=0f8b84cf243295e8cce5fe6ebb104ebb&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202008%2F15%2F20200815203046_pwcze.thumb.400_0.jpeg");
            info.setVideoUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601013182973&di=0f8b84cf243295e8cce5fe6ebb104ebb&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202008%2F15%2F20200815203046_pwcze.thumb.400_0.jpeg");
            info.setMediaState(0);
            info.setMsgRead(0);
            info.setStatus(1);
            info.setMsg("测试发送消息哈哈哈>> " + j);
            list.add(info);
            j++;
        }
        if (addToTop) {
            for (MessageInfo info : list) {
                chatAdapter.addToStart(info);
                linearLayoutManager.scrollToPosition(0);
            }
        } else {
            for (MessageInfo info : list) {
                chatAdapter.addToEnd(info);
                //linearLayoutManager.scrollToPosition(0);
            }
        }
    }

    private void getReceiverList(int num) {
        List<MessageInfo> list = new ArrayList<>();
        if (num < 0) return;
        for (int i = 0; i < num; i++) {
            MessageInfo info = new MessageInfo();
            info.setId("s" + i);
            info.setMsgId("s" + System.currentTimeMillis());
            info.setFromUid("1123");
            info.setTime(System.currentTimeMillis());
            info.setFromUName("发送者3号");
            info.setFromHeadImg("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601013182975&di=6cde0f627a7961021d58658536d4dc7f&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F12%2F20200612180401_mvgks.thumb.400_0.jpeg");
            info.setTargetUid("2311");
            info.setTargetUName("接收者4号");
            info.setTargetHeadImg("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601013182974&di=9105e31fe73643913af3edf17695a969&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202008%2F11%2F20200811231800_rqlkt.thumb.400_0.jpeg");
            info.setTargetType(3);
            info.setType(24);
            info.setImgThumbUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601013182973&di=cdc198d83ce7a81b9295f38848593b72&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202003%2F13%2F20200313135704_xlgyg.thumb.400_0.jpeg");
            info.setImgUrl("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=387639067,1599589691&fm=26&gp=0.jpg");
            info.setAudioDuration(56);
            info.setAudioUrl("https://pic.ibaotu.com/19/55/32/55K888piCGWp.mp3");
            info.setVideoThumbUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601013182973&di=0f8b84cf243295e8cce5fe6ebb104ebb&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202008%2F15%2F20200815203046_pwcze.thumb.400_0.jpeg");
            info.setVideoUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601013182973&di=0f8b84cf243295e8cce5fe6ebb104ebb&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202008%2F15%2F20200815203046_pwcze.thumb.400_0.jpeg");
            info.setMediaState(0);
            info.setMsgRead(0);
            info.setStatus(2);
            info.setMsg("测试接收消息哈>> " + k);
            list.add(info);
            k++;
        }
        if (addToTop) {
            for (MessageInfo info : list) {
                chatAdapter.addToStart(info);
                linearLayoutManager.scrollToPosition(0);
            }
        } else {
            for (MessageInfo info : list) {
                chatAdapter.addToEnd(info);
                //linearLayoutManager.scrollToPosition(0);
            }
        }
    }
}
